###############################################################################
##########02. Salience and Controls############################################
###############################################################################

####Bormann and Golder Data####

#Load Data#

library(rio)

bg <- import("Bormann and Golder/es_data-v3.csv")

#Adjust UK to Great Britain for matching#

bg$country[bg$country == "United Kingdom"] <- "Great Britain"

#Keep only relevant Data #

merge.bg <- bg %>%
  filter(presidential == 0)%>%
  dplyr::select(country, year, legislative_type, enep, enep_others, enep1, regime, tier1_avemag) %>%
  rename(electionyear = year) %>%
  rename(country.name = country)

#Merge Data#

pols <- left_join(pols, merge.bg, by = c("country.name", "electionyear"))

pols$logmag <- log(pols$tier1_avemag)

####Time Dummies####

#Euro Crisis - 2007 and after

pols$eucris <- ifelse(pols$electionyear >= 2007 & pols$electionyear< 2015, 1, 0)

#Refugee Crisis - 2015 and after

pols$refcris <- ifelse(pols$electionyear >= 2015, 1, 0)

####Salience from CMP (Stoll 2010)####

library(manifestoR)

#set key to access data

mp_setapikey(key.file = "manifesto_apikey.txt")

#Download data

cmp <- mp_maindataset(version = "2018b")

#Adjust UK to GB#

cmp$countryname[cmp$countryname == "United Kingdom"] <- "Great Britain"

#calcualte average salience of economics and galtan for each country-election

merge.cmp <- cmp %>%
  tidyr::separate(edate, c("year", "month", "day")) %>%
  filter(year >= 1996) %>%
  mutate(socioeconomic = per401 + per402 + per403 + per404 + per406 + per407 + per408 + per409 + per410 + per411 + per412 + per413 + per414 + per415 + per503 + per504 + per505 + per506 + per507 + per701 + per702 + per703) %>%
  rename(electionyear = year) %>%
  rename(country.name = countryname) %>%
  group_by(country.name, electionyear) %>%
  summarize(socioeconomicsalw = weighted.mean(socioeconomic, pervote, na.rm = T),
            socioeconomicsal = mean(socioeconomic, na.rm = T))

#Adjust electionyear class

merge.cmp$electionyear <- as.numeric(merge.cmp$electionyear)

#Merge

pols <- left_join(pols, merge.cmp, by = c("country.name", "electionyear"))


####Union Density and turnout####

#Download Armgineon et al.'s Comparative Political Data Set

cpd <- import("CPDS-1960-2016-Update-2018.dta")

library(imputeTS)

#Select relevant variables and interpolate missing data#

merge.cpd <- cpd %>%
  filter(year >=1995) %>%
  filter(country == "Austria" | country == "Belgium" | country == "Czech Republic" | country == "Denmark" | country == "Estonia" | country == "Finland" | country == "France" | country == "Germany" | country == "United Kingdom" | country == "Greece" | country == "Hungary" | country == "Ireland" | country == "Italy" | country == "Lithuania" | country == "Luxembourg" |  country == "Netherlands" | country == "Poland" | country == "Portugal" | country == "Romania" | country == "Slovakia" | country == "Slovenia" | country == "Spain" | country == "Sweden") %>%
  dplyr::select(year, country, ud,adjcov, realgdpgr, openc, unemp, vturn)%>%
  rename(country.name = country, electionyear = year) %>%
  group_by(country.name) %>%
  mutate(ud_int = na.interpolation(ud,method = "linear"),
         adjcov_int = na.interpolation(adjcov, method = "linear"))

#Change UK to GB#

merge.cpd$country.name[merge.cpd$country.name == "United Kingdom"] <- "Great Britain"

#merge cpd data with pols data#

pols <- left_join(pols, merge.cpd, by = c("country.name", "electionyear"))

#####Inequality (SWIID from Solt)#####


load("swiid7_0.rda")

swiid_merge <- swiid_summary %>%
  dplyr::select(country, year, gini_disp, gini_mkt) %>%
  dplyr::rename(country.name = country,
                electionyear = year)

swiid_merge$country.name <- ifelse(swiid_merge$country.name == "United Kingdom", "Great Britain", swiid_merge$country.name)

pols  <- left_join(pols, swiid_merge, by = c("country.name", "electionyear"))